<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class CategoryController extends AdminModuleController 
{	
	function _category($model)
	{
		$categorys = $this->CategoryModel->findAll("parentId='0'");
		$r = array();
		foreach($categorys as $v) {
			$r[] = $v;			
			$arr = $this->CategoryModel->findAll("parentId='{$v['categoryId']}'");
			foreach($arr as &$v2){
				$v2['cateName'] = "&nbsp;&nbsp;&nbsp;&nbsp;--".$v2['cateName'];
				$v2['storeId'] = Lookup::item('categoryModel',$v2['storeId']);
			}
			$r = array_merge($r, $arr);
		}
		return $r;
	}

	public function actionAdmin() {
		foreach(Lookup::items('categoryModel') as $key=>$val) {
			$listArr[$key] = array(
				'level' => 0, 
				'child' => $this->CategoryModel->count("storeId='{$key}'"), 
				'cateName' => $val, 
				'listArr' => $this->CategorysPublic->get_list($key)
			);
		}
		$this->view->assign('categorys', $listArr);
		$this->display('category_admin');
	}
	
	public function actionCreate() {
		if(isset($_POST['category'])) {
			$_POST['category']['createdTime'] = time();
			$this->CategoryModel->insert($_POST['category']);
			$this->redirect('category-admin.htm');
		}

		$listArr = array();
		foreach(Lookup::items('categoryModel') AS $key => $val) {
			$listArr[$key] = array(
				'cateName' => $val,
				'listArr' => $this->CategorysPublic->get_options($key)
			);
		}

		$this->view->assign('categoryModel', Lookup::items('categoryModel'));
		$this->view->assign('categoryList', $listArr);
		$this->view->assign('action', 'create');
		$this->display('category_update');
	}
	
	public function actionUpdate() {
		$categoryId = getgpc(2);
		
		if(isset($_POST['category'])) {
			$categoryId = getgpc('categoryId', 'P');
			$categoryArr = $_POST['category'];
			$model = $this->CategoryModel->findByPk($categoryId);
			if($_FILES['picture']['name']) {
				if($model['picture']) {
					@unlink(GODHOUSE_ROOT.substr($model['picture'],	1));
				}
	
				$upload	= new Upload();
				$upfile	= $upload->upfile($_FILES['picture'], 'upfile/category');
				$categoryArr['picture']	= $upfile;
			}
			$categoryArr['createdTime'] = time();
			$this->CategoryModel->updateByPk($categoryId, $categoryArr);
			$this->redirect("category-update-$categoryId.htm");
		}
		
		$category = $this->CategoryModel->findByPk($categoryId);

		$listArr = array();
		foreach(Lookup::items('categoryModel') AS $key => $val) {
			$listArr[$key] = array(
				'cateName' => $val,
				'listArr' => $this->CategorysPublic->get_options($key)
			);
		}
		$this->view->assign('categoryModel', Lookup::items('categoryModel'));
		$this->view->assign('categoryId', $categoryId);
		$this->view->assign('category', $category);
		$this->view->assign('categoryList', $listArr);
		$this->view->assign('action', 'update');
		$this->display('category_update');
	}
	
	public function actionDelete() {
		$categoryId = getgpc(2);
		$this->CategoryModel->deleteByPk($categoryId);
		$this->redirect('category-admin.htm');
	}
}
?>